<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Model\Admin\Admin;
use App\Model\Distributors;
use App\Model\Station;
use App\Model\StationClass;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;
use App\Model\Normal;
use App\Controller\AbstractController;

class StationClassController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;


    #[Inject]
    protected StationClass $stationClass;

    #[Inject]
    protected Admin $admin;
    #[Inject]
    protected Distributors $distributors;

    #[Inject]
    protected Station $station;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**电站列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getList(RequestInterface $request):ResponseInterface
    {
        $page = $request->inputs(['page','limit','keyword','distributor_id']);
        $page['info'] = $this->admin->getInfo($request);
        $query =$this->stationClass->query();
        if(isset($page['info']['distributor_id'])){
            $query->where('distributor_id',$page['info']['distributor_id']);
        }
        if(isset($page['distributor_id']) && $page['distributor_id']){
            $query->where('distributor_id',$page['distributor_id']);
        }
        if($page['keyword']){
            $query->where('label','like','%'.$page['keyword'].'%');
        }
        $query->with('distributor');
        $data['total'] = $query->count();
        $data['list'] = $query->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])->get();
        return $this->success($data);
    }

    /**
     * 所有电站列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function getAllList(RequestInterface $request):ResponseInterface
    {
        $jwt_data = $this->admin->getInfo($request);
        $info = $this->admin->find($jwt_data['id'],['roles_id','station_class']);
        $class_id = $request->input('class_id', 0);
        if($info['roles_id']>0){
            $list = $this->station
                ->whereIn('id',$info['station_class'])
                ->where('class_id',$class_id)
                ->get();
        }else{
            $list = $this
                ->station
                ->where('class_id',$class_id)
                ->get();
        }
        return $this->success($list);
    }



    /**创建电站分组
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function create(RequestInterface $request):\Psr\Http\Message\ResponseInterface
    {
        $data = $request->post();
        $info = $this->admin->getInfo($request);
        if(!isset($data['distributor_id']) || !$data['distributor_id']){
            if ($info['type'] == Admin::TYPE_USER) {
                $data['distributor_id'] = $this->distributors
                    ->where('mobile', $info['mobile'])
                    ->value('id');
            }else{
                return $this->error(1001,'请选择经销商');
            }
        }
        if (isset($data['id']) && $data['id'] > 0) {
            $id = $data['id'];
            unset($data['id']);
            $this->stationClass->where('id', $id)->update($data);
        } else {
            StationClass::create($data);
        }
        return $this->success();
    }

    /**删除分组
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function del(RequestInterface $request):\Psr\Http\Message\ResponseInterface
    {
        $id = $request->input('class_id',0,'int');
        if (!$id) {
            return $this->error(ErrorCode::PARAMS_ERR);
        }
        $this->station->where('class_id',$id)->update(['class_id'=>0]);
        $this->stationClass->where('id',$id)->delete();
        return $this->success();
    }

    public function stations(RequestInterface $request):\Psr\Http\Message\ResponseInterface{
        $page = $request->inputs(['class_id','page','limit']);
        if(!$page['class_id']){
            return $this->success();
        }
        $data = $this->station->where('class_id',$page['class_id'])
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->get();
        return $this->success($data);
    }




}
